unit DataBaseUtils;

interface

uses
  Classes, SysUtils, Forms, Types, DB, ADODB, Grids, DBGrids;

const
  cDBName = 'DataBase.mdb';

type
  TDataBaseTable = class
  public
    Grid : TDBGrid;
    DataSource : TDataSource;
    TableHandle : TADOTable;
    procedure Enable(const TableName : String=''); virtual;
    procedure Disable; virtual;
    constructor Create(const aConnection : TADOConnection; TablePos, TableSize : TPoint; aForm : TForm; aName : String); virtual;
    destructor destroy(); override;
  end;

var
  BaseConnectionStr : String;

implementation

resourcestring
  TableNameError = 'Table Name Is Empty';

{ TDBRec }

constructor TDataBaseTable.Create(const aConnection : TADOConnection; TablePos, TableSize : TPoint; aForm : TForm; aName : String);
begin
  Grid := TDBGrid.Create(aForm);
  with Grid do
  begin
    Parent := aForm;
    Width := TableSize.X;
    Height := TableSize.Y;
    Left := TablePos.X;
    Top := TablePos.Y;
  end;
  TableHandle := TADOTable.Create(Grid);
  DataSource := TDataSource.Create(Grid);
  //
  TableHandle.Connection := aConnection;
  Grid.DataSource := DataSource;
  DataSource.DataSet := TableHandle;
  //
  Grid.Hint := aName;
  Grid.ShowHint := true;
end;

destructor TDataBaseTable.destroy;
begin
  DataSource.Free;
  TableHandle.Free;
  Grid.Free;
  inherited;
end;

procedure TDataBaseTable.Disable;
begin
  TableHandle.Active := false;
end;

procedure TDataBaseTable.Enable(const TableName : String='');
begin
  if (TableName='') and (TableHandle.TableName='') then
  begin
    raise Exception.Create(TableNameError);
    Exit;
  end;
  TableHandle.TableName := TableName;
  TableHandle.Active := true;
end;

end.
